package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyShopAlbumList;
import zhihuilan.mapper.MyShopAlbumListMapper;
import zhihuilan.params.query.MyShopAlbumListQueryParam;
import zhihuilan.service.MyShopAlbumListService;

import java.util.List;

@Service
public class MyShopAlbumListServiceImpl extends ServiceImpl<MyShopAlbumListMapper, MyShopAlbumList> implements MyShopAlbumListService {

    @Override
    public WxMiniPageDto<MyShopAlbumList> selectPageList(MyShopAlbumListQueryParam queryParam) {
        QueryWrapper<MyShopAlbumList> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getRemark())) {
            queryWrapper.eq("remark", queryParam.getRemark());
        }
        if(!ObjectUtils.isEmpty(queryParam.getPid())){
            queryWrapper.eq("pid", queryParam.getPid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        if(!ObjectUtils.isEmpty(queryParam.getStatus())){
            queryWrapper.eq("status", queryParam.getStatus());
        }else{
            queryWrapper.eq("status", 1);
        }

        queryWrapper.orderByDesc("addtime");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyShopAlbumList> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyShopAlbumList> result = new WxMiniPageDto(page.getCurrent(), page.getSize());
        BeanUtils.copyProperties(page, result);
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public List<MyShopAlbumList> selectList(MyShopAlbumListQueryParam queryParam) {
        QueryWrapper<MyShopAlbumList> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getRemark())) {
            queryWrapper.eq("remark", queryParam.getRemark());
        }
        if(!ObjectUtils.isEmpty(queryParam.getPid())){
            queryWrapper.eq("pid", queryParam.getPid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        if(!ObjectUtils.isEmpty(queryParam.getStatus())){
            queryWrapper.eq("status", queryParam.getStatus());
        }else{
            queryWrapper.eq("status", 1);
        }
        queryWrapper.orderByAsc("sort");
        List<MyShopAlbumList> selectList = this.baseMapper.selectList(queryWrapper);
        return selectList;
    }

    @Override
    public MyShopAlbumList selectById(Integer id) {
        MyShopAlbumList result = this.baseMapper.selectById(id);
        return result;
    }

    @Override
    public MyShopAlbumList addEntity(MyShopAlbumList entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyShopAlbumList updateEntity(MyShopAlbumList entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public Boolean deletetById(Integer id) {
        int result = this.baseMapper.deleteById(id);
        return result > 0 ? true : false;
    }

    @Override
    public Boolean deleteByIds(List<Integer> ids){
        if(ObjectUtils.isEmpty(ids)||ids.size()==0){
            return false;
        }
        QueryWrapper<MyShopAlbumList> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        int result = this.baseMapper.delete(queryWrapper);
        return result > 0 ? true : false;
    }
}
